//
//  MYStatusRetweetedView.swift
//  MYWeibo
//
//  Created by Magic Yu on 16/4/17.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit
import SnapKit

class MYStatusRetweetedView: UIView {
    
    //底部约束
    var bottomCons: Constraint?
    var statusViewModel: MYStatusViewModel? {
        didSet {
        textLabel.text = statusViewModel?.status?.retweeted_status?.text
         
            self.bottomCons?.uninstall()
            if let url = statusViewModel?.status?.retweeted_status?.pic_urls where url.count > 0 {
            
                //有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(picView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源
                picView.imageURLs = url
                //视图不隐藏
                picView.hidden = false
            }else {
            
                //没有配图视图
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(textLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //设置数据源
                picView.imageURLs = nil
                //视图隐藏
                picView.hidden = true
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI界面
    private func setupUI() {
        
        //设置灰度色
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        
        addSubview(textLabel)
        addSubview(picView)
        
        //设置约束
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(textLabel.snp_left)
        }
        
        //转发微博行高自动计算 约束底部
        self.snp_makeConstraints { (make) -> Void in
           self.bottomCons = make.bottom.equalTo(textLabel.snp_bottom).offset(StatusCellMargin).constraint
        }
        
    }
    
    //MARK: 懒加载子视图
    private lazy var textLabel = UILabel(text: "我是一条转发的微博O(∩_∩)O哈哈~", textColor: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMargin)
    /// 配图视图
    private lazy var picView: MYStatusPictureView = MYStatusPictureView()
}
